package com.compass.doctor.ui.chat.viewholder;

import static com.compass.framework.utils.DisplayUtil.dpToPx;
import static com.compass.framework.utils.ResUtilsKt.getDrawableResource;

import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.compass.doctor.R;
import com.compass.doctor.databinding.ChatMessageArticeViewHolderBinding;
import com.compass.doctor.ui.chat.attachment.ArticleAttachment;
import com.compass.doctor.ui.chat.attachment.InquiryAttachment;
import com.compass.framework.utils.LogUtil;
import com.compass.framework.utils.ViewUtils;
import com.google.gson.Gson;
import com.netease.yunxin.kit.chatkit.ui.common.MessageHelper;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.fun.view.message.viewholder.FunChatBaseMessageViewHolder;
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean;

/**
 * Description： 文章消息UI
 * Date:2024/1/19 10:55
 */
public class ArticleViewHolder extends FunChatBaseMessageViewHolder {
    public static final String TAG = "InquiryViewHolder";
    ChatMessageArticeViewHolderBinding binding;
    ArticleAttachment attachment;

    public ArticleViewHolder(@NonNull ChatBaseMessageViewHolderBinding parent, int viewType) {
        super(parent, viewType);
    }

    @Override
    protected void addViewToMessageContainer() {
        binding = ChatMessageArticeViewHolderBinding.inflate(LayoutInflater.from(parent.getContext()), getMessageContainer(), true);

    }


    @Override
    public void bindData(ChatMessageBean message, ChatMessageBean lastMessage) {
        super.bindData(message, lastMessage);
        currentMessage = message;
        attachment = (ArticleAttachment) message.getMessageData().getMessage().getAttachment();
        if (attachment == null) {
            return;
        }
        //设置ViewHolder展示UI的内容
//
        binding.title.setText(attachment.getTitle());
        Glide.with(parent.getContext()).load(attachment.getConverImg())
                .skipMemoryCache(false) //启用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略
                .into(binding.imgArticle);
        ViewUtils.INSTANCE.setClipViewCornerRadius(binding.imgArticle,dpToPx(5));

        Glide.with(parent.getContext()).load(attachment.getDoctorAvatar())
                .skipMemoryCache(false) //启用内存缓存
                .error(com.compass.framework.R.mipmap.default_head)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略
                .apply(RequestOptions.circleCropTransform()) // 圆形
                .into(binding.imgDoctorHead);

        binding.tvDoctorName.setText(attachment.getDoctorName());
        binding.tvHospitalName.setText(attachment.getDoctorTitle());
    }



    @Override
    protected void onMessageBackgroundConfig(ChatMessageBean messageBean) {
        super.onMessageBackgroundConfig(messageBean);
        boolean isReceivedMsg = MessageHelper.isReceivedMessage(messageBean);
        View firstChild = baseViewBinding.messageContainer.getChildAt(0);
        if (isReceivedMsg) {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message);
        } else {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message_out);
        }
    }
}
